package net.huashitong.Indicators.service;

import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.dto.BA01DTO;
import net.huashitong.Indicators.entity.AB01;
import net.huashitong.Indicators.entity.AB02;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BattingService {

    @Autowired
    private AB02Service ab02Service;

    @Autowired
    private AB01Service ab01Service;

    @Autowired
    private RedisService redisService;
    /**
     * 保存投注信息
     * @param ba01DTO
     */
    public void saveAb02s(BA01DTO ba01DTO)
    {
        AB02 ab02 = new AB02();
        ab02.setBAB001(ba01DTO.getBAB001());
        //ab02.setAab999(ba01DTO.getAab999());
        ab02.setBAB205(redisService.get("semesterDate"));//保存选课学期
        ab02.setAAB001(ba01DTO.getAAB001());
        ab02.setAAB996(ba01DTO.getAAB996());
        ab02.setAAB998(ba01DTO.getAAB998());
        ab02.setBAB003(ba01DTO.getBAB003());
        ab02.setAAA001("0");//默认0，未被选上   1选上，2 退课
        ab02Service.save(ab02);

    }
    //修改账户余额
    public  void updateMemey(String aab996,String userId,String aab001) throws Exception {

        AB01 ab01 = ab01Service.queryStudent(aab001,userId);
        if(ObjectUtils.isNotBlank(ab01))
        {
            Integer meney  = Integer.parseInt(ab01.getAAB999())-Integer.parseInt(aab996);
            if (meney<0)
            {
                throw new Exception();
            }

            ab01.setAAB999(meney.toString());
        }

    }
    //更新选课记录
    public void updateAb02(AB02 ab02,String aab996,String userid) throws Exception {
        Integer moneyold =Integer.parseInt( ab02.getAAB996());
        ab02.setAAB996(aab996);
        ab02Service.update(ab02);
        moneyold=Integer.parseInt(aab996)-moneyold;
        updateMemey(moneyold.toString(),userid,ab02.getAAB001());
    }
    /**
     * 回血
     * @param ab02
     * @param aab996
     * @param userid
     */
    public void updateAcountBack(AB02 ab02,String aab996,String userid)
    {
        Integer moneyold =Integer.parseInt( ab02.getAAB996());
        ab02.setAAB996(aab996);
        ab02.setAAA001("2");//退课了
        ab02Service.update(ab02);
        moneyold=Integer.parseInt(aab996)-moneyold;
        AB01 ab01 = ab01Service.queryStudent(ab02.getAAB001(),userid);
        Integer meney  = Integer.parseInt(ab01.getAAB999())-moneyold;
        ab01.setAAB999(meney.toString());
    }


}
